package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门模块controller
 */
@RestController
@RequestMapping("/depts")
@Slf4j
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result list() {
        log.info("部门controller，查询列表");
        // 1. 获取请求，调用service
        List<Dept> list = deptService.findAll();
        // TODO 此处代码未完成，需要后续添加
        // 添加了一个功能
        // 2. 封装结果，返回
        return Result.success(list);
    }

    @PostMapping
    public Result save(@RequestBody Dept dept) {
        log.info("部门controller，添加");
        try {
            deptService.save(dept);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("保存失败");
        }
        return Result.success();
    }
}
